package com.cqwu.conf;

import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
 * 
* @Title: KeyValue
* @Description: 读取keyvalue.xml配置文件
* @Company: 物联网俱乐部
* @author: lj
* @date: Nov 17, 2015 3:56:14 PM
 */
public class KeyValue {
	/**
	 * 
	 * @param name：配置文件keyvalue.xml中的二级节点名（即：模块对应的标签名）
	 *             
	 * @return：          该二级节点下的所有的内容
	 */
	@SuppressWarnings("unchecked")
	public static String[] getListByName(String name) {
		String[] str = null;
		int i = 0;
		try {
			SAXReader reader = new SAXReader();
			String xmlpath="/com/cqwu/conf/"+"keyvalue.xml";
			ClassLoader classLoader=KeyValue.class.getClassLoader();
			InputStream in=classLoader.getResourceAsStream(xmlpath);
			Document document = reader.read(in);
			List<Element> elements = document.selectNodes("/config/" + name);		
			Element m = elements.get(0);
			List ls = m.elements();
			str = new String[ls.size()];
			for (Iterator<Element> it = ls.iterator(); it.hasNext();) {
				Element e = (Element) it.next();
				str[i++] = e.getText();
			}

		} catch (Exception e) {
			throw new RuntimeException("XML读取失败");
		}
		return str;
	}
	
	public static void main(String[] args) {
		String[] arr = getListByName("effect");
		System.out.println(arr[1]);
	}
}
